package jack.com.servicekeep;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import jack.com.servicekeep.utils.AppUtil;

public class QuitActivity extends Activity implements View.OnClickListener {

    private Button btCopy;
    private TextView tvPhoneId;
    private TextView btCommit;
    private View btSetWifi;
    private EditText etInputCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quit);

//        ActionBar actionBar = getActionBar();
//        actionBar.setDisplayHomeAsUpEnabled(true);
//        actionBar.setTitle("");

        btCommit = findViewById(R.id.bt_commit);
        btCommit.setOnClickListener(this);

        btCopy = findViewById(R.id.bt_copy);
        btCopy.setOnClickListener(this);

        btSetWifi = findViewById(R.id.bt_set_wifi);
        btSetWifi.setOnClickListener(this);

        tvPhoneId = findViewById(R.id.tv_phone_id);

        etInputCode = findViewById(R.id.et_input_code);

        setUiView();
    }

    private void setUiView() {
        tvPhoneId.setText(AppUtil.getDeviceUUID(getApplicationContext()));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:   //返回键的id
                this.finish();
                return false;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_commit:
                String input = etInputCode.getText().toString();
                if(input.isEmpty()){
                    Toast.makeText(this, "请输入解锁码", Toast.LENGTH_SHORT).show();
                    return;
                }
                String pwd = AppUtil.getCancelPwd(getApplicationContext());
                Log.d("yanjun", "Quit: pwd="+pwd + " inputs="+input);
                if(pwd.equalsIgnoreCase(input)){
                    AppUtil.saveDingState(getApplicationContext(), 2);
                    Toast.makeText(this, "解锁成功，现在可以退出学习模式", Toast.LENGTH_SHORT).show();
                    AppUtil.hideInput(this);
                    setResult(RESULT_OK);
                    finish();
                }else{
                    Toast.makeText(this, "无效解锁码", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bt_copy:
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData mClipData = ClipData.newPlainText("PhoneId", tvPhoneId.getText());
                cm.setPrimaryClip(mClipData);

                Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_set_wifi:
                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                break;
        }
    }
}
